package org.stan.beanvalidator.factory;

import org.stan.beanvalidator.annotation.*;
import org.stan.beanvalidator.annotation.Number;
import org.stan.beanvalidator.factory.validator.*;
import org.stan.beanvalidator.utils.ValidateConstant;

import java.lang.reflect.Field;

/**
 * 实体类校验工厂
 * @author src
 * @Date 2020/6/9 10:50
 * @version 1.0
 */
public class BeanValidateFactory {

    /**
     * 抽象工厂获取工厂生产者(旧方法，先保留)
     * @author src
     * @Date 2020/6/9 10:50
     * @version 1.0
     */
    public AnnotationFactoty getValidator(Field field){
        if(field.isAnnotationPresent(NotEmpty.class)){
            // 非空校验器
            return new NotEmptyValidator();
        }else if(field.isAnnotationPresent(IDCard.class)){
            // 身份证校验器
            return new IDCardValidator();
        }else if(field.isAnnotationPresent(Email.class)){
            // 邮箱校验器
            return new EmailValidator();
        }else if(field.isAnnotationPresent(Tel.class)){
            // 座机号码校验器
            return new TelValidator();
        }else if(field.isAnnotationPresent(Mobile.class)){
            // 手机号码校验器
            return new MobileValidator();
        }else if(field.isAnnotationPresent(Number.class)){
            // 数字验证器
            return new NumberValidator();
        }else if(field.isAnnotationPresent(Pattern.class)){
            // 自定义校验器
            return new PatternValidator();
        }else if(field.isAnnotationPresent(PostCode.class)){
            // 邮编校验器
            return new PostCodeValidator();
        }else{
            return new DefaultValidator();
        }
    }

    /**
     *@Description 重载方法：策略工厂
     *@Author src
     *@Date 2020-07-04 8:52
     *@Param
     *@return
     */
    public static AnnotationFactoty getValidator(String ann){
        return BeanValidateSingletonFactory.getInstance().getMap().get(ann);
    }


}
